package com.zc.auth.security.core.configuration.adapter;

import com.zc.auth.security.core.authentication.AuthenticationTokenService;
import com.zc.auth.security.core.filter.ServletAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.DefaultLoginPageConfigurer;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

/**
 * 基于Servlet的配置增强 {@link WebSecurityConfigurerAdapter}
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/1/12 16:30
 */
public class WebAuthSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    private FilterInvocationSecurityMetadataSource securityMetadataSource;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;
    @Autowired
    private AccessDecisionManager accessDecisionManager;
    @Autowired
    private AuthenticationTokenService authenticationTokenService;


    @Override
    final protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .cors()
                .and()
                .authorizeRequests().anyRequest().authenticated().withObjectPostProcessor(filterSecurityInterceptorObjectPostProcessor())
                .and()
                .exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .and()
                .addFilterAt(authenticationFilter(), BasicAuthenticationFilter.class)
                .httpBasic();
        http.removeConfigurer(DefaultLoginPageConfigurer.class);
        this.customerConfigure(http);
    }

    /**
     * 子类自定义配置
     *
     * @param http
     */
    protected void customerConfigure(HttpSecurity http) {

    }

    /**
     * @return
     * @description 设置元数据
     */
    private ObjectPostProcessor<FilterSecurityInterceptor> filterSecurityInterceptorObjectPostProcessor() {
        return new ObjectPostProcessor<FilterSecurityInterceptor>() {
            @Override
            public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                object.setAccessDecisionManager(accessDecisionManager);
                object.setSecurityMetadataSource(securityMetadataSource);
                return object;
            }
        };
    }

    /**
     * 添加校验滤器
     *
     * @return
     * @throws Exception
     */
    private ServletAuthenticationFilter authenticationFilter() throws Exception {
        return new ServletAuthenticationFilter(authenticationManager()).tokenService(authenticationTokenService);
    }


}
